<?
include '../config.inc.php';
$username = @$_POST['username'];
$password = @$_POST['password'];
$cion = base64_decode(@$_POST['cion']);

if($cion < '0.01'){
   echo '你不符合领取红包的条件';
   exit;
}
include '../member.php';
//先判断有没有红包
$redball = mysql_query("SELECT * FROM wwk_chat_redball WHERE type='0'");
$red = mysql_fetch_array($redball);
//1为抢完 0为正常
if($red['type'] == ''){
  echo '红包已经没有了,等待管理员发放红包。';
  exit;
}else{
	//判断红包状态 1没有 0正常
	 if($red['type'] == '1'){
        echo '红包已经没有了。等待管理员发放红包。';
	    exit;
	 }
//判断红包状态 1没有 0正常 [结束]
    //红包是否被领完
	 if($red['last_number'] == '0'){
        echo '你手太慢了，红包已经被领完！';
	    exit;
	 }
	//判断此用户有没有领取过这个红包
   $redball_log = mysql_query("SELECT * FROM wwk_chat_redball_log WHERE username='{$username}' and red_id='{$red['id']}'");
   $red_log = mysql_fetch_array($redball_log);
    if($red_log['red_id'] != ''){
	   echo '你已经领取过这个红包啦';
	   exit;	
	}
//判断此用户有没有领取过这个红包 [结束]

		//如果没有领取过这个红包    
                          //红包类型
                          if($red['red_type'] == '0'){
							    //平分红包
							 $money = $red['money'] / $red['number'];
                           }else{
                                  //随机红包
						   }
              //更新红包数据
                  if($red['last_number'] == '1'){
                  if(!mysql_query("update wwk_chat_redball set last_number='0',type='1' WHERE id='{$red['id']}'")){
					  echo '更新红包错误，终止抢红包';
					  exit;
					}
				  }else{
				  $lastnumber = $red['last_number'] - 1;
                  if(!mysql_query("update wwk_chat_redball set last_number='{$lastnumber}' WHERE id='{$red['id']}'")){
					  echo '更新红包错误，终止抢红包';
					  exit;
					}
				  }

				//开始插入领取红包记录表
				 //insert red_log
                    if(	!mysql_query("INSERT INTO wwk_chat_redball_log (red_id , username,nickname,red_money) VALUES ('{$red['id']}','$username','$nickname','$money')")){
					  echo '写入log错误，终止抢红包';
					  exit;
					}

	          //插入会员表
					$ssc_coin = mysql_query("SELECT * FROM ssc_members WHERE username='{$username}'");
                    $ssc_data = mysql_fetch_array($ssc_coin);
                    $moneys = $ssc_data['coin'] + $money;
					if($moneys ==''){
					   echo '抢红包失败';
					   exit;
					}
					//insert  平台上的表
                  if(!mysql_query("update ssc_members set coin='{$moneys}' WHERE username='{$username}'")){
					  echo '更新红包错误，终止抢红包';
					  exit;
					}

				//判断是否插入成功
				 
					/* echo '发红包人：'.$red['nickname'].'<br>';
                      echo '红包金额：'.$red['money'].'<br>';
                      echo '红包个数：'.$red['number'].'<br>';
                      echo '还剩个数：'.$red['last_number'].'<br>';*/
	           
					  echo '恭喜您,抢到 '.$money.' 元的红包。';
 }
?>


<script>
function send_redball_log(){
var username  = '<? echo $username;?>';
var password  = '<? echo $password;?>';
var user_vip  = '<? echo $user_vip;?>';

var sendvalue = '<div class="inner RedPackRecipt"><p><i class="iconfont icon-hongbao-copy"></i><span class="t-red"><?echo $nickname;?></span> 抢到了金额为<span class="t-red"><?echo $money;?></span>元的红包<br>红包还剩：<?echo $red['last_number'] - 1;?>个<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;系统消息</p></div>';
$.post("send.php", { sendvalue: sendvalue,username: username,password: password,user_vip: user_vip,cion: '1000'},
   function(data){
	//返回值
   layer.msg(data);
   });
}
send_redball_log();
</script>